package src.Packets;

import java.io.IOException;
import src.Util.Stream;
import src.Players.Player;
import src.Main;

public class Packet extends HandlePacket
{
    private int[] packetSizes =
    {
        0, 0, 0, 1, -1, 0, 0, 0, 0, 0, //0
        0, 0, 0, 0, 8, 0, 6, 2, 2, 0,  //10
        0, 2, 0, 6, 0, 12, 0, 0, 0, 0, //20
        0, 0, 0, 0, 0, 8, 4, 0, 0, 2,  //30
        2, 6, 0, 6, 0, -1, 0, 0, 0, 0, //40
        0, 0, 0, 12, 0, 0, 0, 0, 8, 0, //50
        0, 8, 0, 0, 0, 0, 0, 0, 0, 0,  //60
        6, 0, 2, 2, 8, 6, 0, -1, 0, 6, //70
        0, 0, 0, 0, 0, 1, 4, 6, 0, 0,  //80
        0, 0, 0, 0, 0, 3, 0, 0, -1, 0, //90
        0, 13, 0, -1, 0, 0, 0, 0, 0, 0,//100
        0, 0, 0, 0, 0, 0, 0, 6, 0, 0,  //110
        1, 0, 6, 0, 0, 0, -1, 0, 2, 6, //120
        0, 4, 6, 8, 0, 6, 0, 0, 0, 2,  //130
        0, 0, 0, 0, 0, 6, 0, 0, 0, 0,  //140
        0, 0, 1, 2, 0, 2, 6, 0, 0, 0,  //150
        0, 0, 0, 0, -1, -1, 0, 0, 0, 0,//160
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  //170
        0, 8, 0, 3, 0, 2, 0, 0, 8, 1,  //180
        0, 0, 12, 0, 0, 0, 0, 0, 0, 0, //190
        2, 0, 0, 0, 0, 0, 0, 0, 4, 0,  //200
        4, 0, 0, 0, 7, 8, 0, 0, 10, 0, //210
        0, 0, 0, 0, 0, 0, -1, 0, 6, 0, //220
        1, 0, 0, 0, 6, 0, 6, 8, 1, 0,  //230
        0, 4, 0, 0, 0, 0, -1, 0, -1, 4,//240
        0, 0, 6, 6, 0, 0, 0            //250
    };

    public Packet()
    {
    }

    public boolean packetProcess(Player p)
    {
        if(p == null || p.in == null || p.inStreamDecryption == null || p.disconnected[0])
        {
            p.packetIndex = -1;
            return false;
        }
        int avail = 0;
        try
        {
            avail = p.in.available();
            if(avail == 0)
            {
                return false;
            }
            if(p.packetIndex == -1)
            {
                p.packetIndex = p.in.read() & 0xff;
            }
            p.packetIndex = p.packetIndex - p.inStreamDecryption.getNextKey() & 0xff;
            p.packetSize = packetSizes[p.packetIndex];
            avail--;
            if(p.packetSize == -1)
            {
                if(avail > 0)
                {
                    p.packetSize = p.in.read() & 0xff;
                    avail--;
                }
                else
                {
                    return false;
                }
            }
            if(avail < p.packetSize)
            {
                return false;
            }
            fillInStream(p.packetSize, p);
            if(p.packetIndex > 0)
            {
                handlePacket(p);
            }
            else
            {
                p.packetIndex = -1;
                return false;
            }
        }
        catch(Exception error)
        {
            p.disconnected[0] = p.disconnected[1] = true;
            p.packetIndex = -1;
            return false;
        }
        p.packetIndex = -1;
        return true;
    }

    private void fillInStream(int forceRead, Player p)
    {
        if(p != null && p.inStream != null && p.in != null)
        {
            try
            {
                p.inStream.resetOffset();
                p.in.read(p.inStream.getBuffer(), 0, forceRead);
            }
            catch(IOException ioe)
            {
            }
        }
    }
}